package com.itheima.webmagic.demo;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.FilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

public class JobProcessor_links implements PageProcessor {

    public void process(Page page) {
        //XPath
        page.putField("xpath", page.getHtml().xpath("//ul[@class=help_list]/li/a").all());
        page.addTargetRequests(page.getHtml().links().regex("(https://help.jd.com/user/issue/32.\\d{4}.*\\.html$)").all());
        page.putField("title",page.getHtml().xpath("//div[@id=pdfContainer]/div/div/text()").all() );
    }

    private Site site = Site.me()/*.setCharset("UTF-8")*///编码
            .setSleepTime(1)//抓取间隔时间
            .setTimeOut(1000*10)//超时时间
            .setRetrySleepTime(3000)//重试时间
            .setRetryTimes(3);//重试次数
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new JobProcessor_links())
                //初始访问url地址
                .addUrl("https://help.jd.com/user/issue/list-28.html")
                .addPipeline(new FilePipeline("D:\\IdeaProject\\localfile\\webmagic_result\\"))
                .thread(5)
                .run();
    }
}